/**
 * ProgramWindow.java
 *
 * @author Justin Cole
 * @author David Marczak
 *
 * Copyright (C) 2009 Justin Cole, Aaron Jankun, David Marczak, Vineet Sharma,
 *        and Aaron Toth
 *
 * This file is part of Wernicke.
 *
 * Wernicke is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package grimace.client;

import java.awt.Component;
import javax.swing.JTabbedPane;
import java.util.ArrayList;

/**
 * ProgramWindow is the main program window. All GUI elements are displayed or
 * parented by it.
 */
public class ProgramWindow extends javax.swing.JFrame {
    static JTabbedPane tabs;

    /** Creates new form Window */
    public ProgramWindow() {
        initComponents();
        splitPane.setRightComponent(null);
        this.setSize(loginForm1.getMinimumSize().width + splitPane.getDividerSize(),this.getHeight());
    }

    public Component getLeftPane() {
        return splitPane.getLeftComponent();
    }
    
    public Component getRightPane() {
        return splitPane.getRightComponent();
    }

    public void setLeftPane(Component comp) {
        // @TODO: Clean up these methods
        splitPane.setLeftComponent(comp);
        if (comp == null) {
            this.setSize(splitPane.getRightComponent().getMinimumSize().width, this.getHeight());
            splitPane.setDividerLocation(0.0);
        } else {
            int width = comp.getMinimumSize().width + splitPane.getDividerSize();
            Component rightComp = splitPane.getRightComponent();
            if (rightComp != null) {
                width += rightComp.getMinimumSize().width;
            }
            this.setSize(width, this.getHeight());
        }
    }

    public void setRightPane(Component comp) {
        splitPane.setRightComponent(comp);
        if (comp == null) {
            this.setSize(splitPane.getLeftComponent().getMinimumSize().width, this.getHeight());
            splitPane.setDividerLocation(1.0);
        } else {
            int width = comp.getMinimumSize().width + splitPane.getDividerSize();
            Component leftComp = splitPane.getLeftComponent();
            if (leftComp != null) {
                width += leftComp.getMinimumSize().width;
            }
            this.setSize(width, this.getHeight());
        }
    }

    public static void updateChatTabs(ArrayList<ChatPanel> chats) {
        tabs = new JTabbedPane();
        java.awt.Dimension min = new java.awt.Dimension();
        min.setSize(600, 420);
        tabs.setMinimumSize(min);
        for (int i = 0; i < chats.size(); i++) {
            ChatPanel chat = chats.get(i);
            tabs.add(chat.getTitle(), chat);
        }
        ProgramController.setRightPane(tabs);
    }

    public static void addChatTab(ChatPanel chat) {
        tabs.add(chat.getTitle(), chat);
        tabs.setSelectedComponent(chat);
    }

    public static void closeTab(Component comp) {
        tabs.remove(comp);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        splitPane = new javax.swing.JSplitPane();
        loginForm1 = new grimace.client.LoginForm();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Wernicke");

        splitPane.setContinuousLayout(true);
        splitPane.setMinimumSize(new java.awt.Dimension(300, 450));
        splitPane.setOneTouchExpandable(true);
        splitPane.setPreferredSize(new java.awt.Dimension(300, 450));
        splitPane.setLeftComponent(loginForm1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 770, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splitPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private grimace.client.LoginForm loginForm1;
    private javax.swing.JSplitPane splitPane;
    // End of variables declaration//GEN-END:variables

}
